package com.yugao.fintech.antelope.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yugao.fintech.antelope.base.utils.ExcelContentType;
import com.yugao.fintech.antelope.base.utils.ExcelUtils;
import com.yugao.fintech.antelope.base.security.annotation.InnerAuth;
import com.yugao.fintech.antelope.base.log.LogTag;
import com.yugao.fintech.antelope.base.log.OperateLog;
import com.yugao.fintech.antelope.base.log.enums.BusinessType;
import com.yugao.fintech.antelope.base.ram.ActionType;
import com.yugao.fintech.antelope.base.ram.RamResource;
import com.yugao.fintech.antelope.system.model.entity.SysOperateLog;
import com.yugao.fintech.antelope.system.model.rqrs.log.OperateLogDeleteReq;
import com.yugao.fintech.antelope.system.model.rqrs.log.OperateLogPageReq;
import com.yugao.fintech.antelope.system.model.rqrs.log.OperateLogSaveReq;
import com.yugao.fintech.antelope.system.service.SysOperateLogService;
import com.yugao.fintech.framework.core.response.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "操作日志管理")
@LogTag(name = "操作日志")
@RestController
@RequestMapping("/operateLog")
public class OperateLogController {
    @Autowired
    private SysOperateLogService operateLogService;

    @ApiOperation("获取日志列表")
    @GetMapping("/page")
    @RamResource(action = "sys:operateLog:query", name = "查询操作日志", type = ActionType.READ)
    public Resp<IPage<SysOperateLog>> page(OperateLogPageReq req) {
        IPage<SysOperateLog> page = operateLogService.pageOperateLog(req);
        return Resp.ok(page);
    }

    @OperateLog(summary = "导出操作日志", businessType = BusinessType.EXPORT)
    @RamResource(action = "sys:operateLog:export", name = "导出操作日志", type = ActionType.READ)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperateLog reqParam) throws Exception {
        List<SysOperateLog> list = operateLogService.listOperateLog(reqParam);
        ExcelUtils.exportExcel(response, ExcelContentType.XLS, "操作日志.xls", SysOperateLog.class, list);
    }

    @ApiOperation("移除日志")
    @OperateLog(summary = "删除操作日志", businessType = BusinessType.DELETE)
    @RamResource(action = "sys:operateLog:delete", name = "删除操作日志", type = ActionType.WRITE)
    @DeleteMapping("/deleteBatch")
    public Resp<?> delete(@RequestParam(value = "ids") Long[] operateIds) {
        return operateLogService.deleteBatch(operateIds) ? Resp.ok() : Resp.fail("删除失败");
    }

    @OperateLog(summary = "删除操作日志", businessType = BusinessType.DELETE)
    @RamResource(action = "sys:operateLog:delete", name = "删除操作日志", type = ActionType.WRITE)
    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestParam(value = "id") Long operateId) {
        return operateLogService.deleteBatch(new Long[] { operateId }) ? Resp.ok() : Resp.fail("删除失败");
    }

    @ApiOperation("清空日志")
    @OperateLog(summary = "清空操作日志", businessType = BusinessType.CLEAN)
    @RamResource(action = "sys:operateLog:clean", name = "清除操作日志", type = ActionType.WRITE)
    @DeleteMapping("/clean")
    public Resp<?> clean() {
        operateLogService.clear();
        return Resp.ok();
    }

    @ApiOperation("定时删除日志")
    @DeleteMapping("/task")
    public Resp<?> delete(OperateLogDeleteReq req) {
        return operateLogService.delete(req) ? Resp.ok() : Resp.fail("删除失败");
    }

    @ApiOperation("添加日志")
    @InnerAuth
    @PostMapping("/save")
    public Resp<?> save(@RequestBody OperateLogSaveReq req) {
        operateLogService.save(req);
        return Resp.ok();
    }
}